package shopping.web;

import shopping.bean.Clothes;
import shopping.bean.Goods;
import shopping.bean.List;
import shopping.bean.ListItem;
import shopping.service.ClothesService;
import shopping.service.GoodsService;
import shopping.service.impl.ClothesServiceImpl;
import shopping.service.impl.GoodsServiceImpl;
import shopping.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

public class ListServlet extends BaseServlet{
    ClothesService clothesService = new ClothesServiceImpl();
    GoodsService goodsService = new GoodsServiceImpl();
    /**
     * 添加商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        int status = WebUtils.parseInt(req.getParameter("status"), 0);
        System.out.println("status=" + status);
        //如果status==0，则为出库列表，反之则为入库列表
        if (status == 0) {
            Clothes clothes = clothesService.queryClothesById(id);

            //把图书信息转换成ListItem商品项
            ListItem listItem = new ListItem(clothes.getId(), clothes.getName(), 1, clothes.getPrice(), clothes.getPrice());
            List cl_list = (List) req.getSession().getAttribute("cl_list");
            if(cl_list == null){
                cl_list = new List();
                req.getSession().setAttribute("cl_list",cl_list);
            }
            //将商品信息放入购物车
            cl_list.addItem(listItem);
            System.out.println(cl_list.getItems());
            //重定向当商品列表
            resp.sendRedirect(req.getHeader("Referer"));
            //保存最后一个添加的书名到Session域中
            req.getSession().setAttribute("cl_lastname",listItem.getName());
        } else {
            Goods goods = goodsService.queryGoodsById(id);
            //如果服装列表中已有该服装信息，则修改数量,反之，则添加该信息
            Clothes clothes = clothesService.queryClothesByName(goods.getName());
            System.out.println(clothes);
            if(clothes == null) {
                clothesService.addClothes(new Clothes(null,goods.getName(),goods.getKind(),goods.getPrice(),0,0,goods.getImgPath()));
            }
            //把图书信息转换成ListItem商品项
            ListItem listItem = new ListItem(goods.getId(), goods.getName(), 1, goods.getPrice(), goods.getPrice());
            List go_list = (List) req.getSession().getAttribute("go_list");
            if(go_list == null){
                go_list = new List();
                req.getSession().setAttribute("go_list",go_list);
            }
            //将商品信息放入购物车
            go_list.addItem(listItem);
            System.out.println(go_list.getItems());
            //重定向当商品列表
            resp.sendRedirect(req.getHeader("Referer"));
            //保存最后一个添加的书名到Session域中
            req.getSession().setAttribute("go_lastname",listItem.getName());

        }

    }

    /**
     * s删除商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        int status = WebUtils.parseInt(req.getParameter("status"), 0);
        if (status == 0){
            //获取购物车对象
            List cl_list = (List) req.getSession().getAttribute("cl_list");
            if(cl_list != null){
                cl_list.deleteItem(id);
            }
        }else{
            //获取购物车对象
            List go_list = (List) req.getSession().getAttribute("go_list");
            if(go_list != null){
                ListItem listItem = go_list.selectItem(id);
                go_list.deleteItem(id);
                //注：当集合中的该服装信息删除后，需要将服装表里的该信息也删除，同理，不能根据id删除
                Clothes clothes = clothesService.queryClothesByName(listItem.getName());
                clothesService.deleteClothesById(clothes.getId());
            }
        }

        //重定向当商品列表
        resp.sendRedirect(req.getHeader("Referer"));
    }


    /**
     * 清空购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
//        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        int status = WebUtils.parseInt(req.getParameter("status"), 0);
        if (status == 0){
            //获取购物车对象
            List cl_list = (List) req.getSession().getAttribute("cl_list");
            if(cl_list != null){
                cl_list.clear();
            }
        }else{
            //获取购物车对象
            List go_list = (List) req.getSession().getAttribute("go_list");
            if(go_list != null){
                go_list.clear();
            }
        }

        //重定向当商品列表
        resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 修改商品数量
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        int status = WebUtils.parseInt(req.getParameter("status"), 0);

        int count = WebUtils.parseInt(req.getParameter("count"), 0);


        if (status == 0){
            List cl_list = (List) req.getSession().getAttribute("cl_list");
            if(cl_list != null){
                cl_list = new List();
                cl_list.updateCount(id,count);
                req.getSession().setAttribute("cl_list",cl_list);
            }
        }else{
            List go_list = (List) req.getSession().getAttribute("go_list");
            if(go_list != null){
                go_list = new List();
                go_list.updateCount(id,count);
                req.getSession().setAttribute("go_list",go_list);
            }
        }

        //重定向当商品列表
        resp.sendRedirect(req.getHeader("Referer"));
    }
}
